#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import bs4
import requests

import re
rx = re.compile(r'\d+(.\d+)?\Z')
def isdigit(s):
    # is it a number?
    return rx.match(s)

URL = "http://fund.eastmoney.com/f10/F10DataApi.aspx?type=lsjz"

data = []
for i in range(1, 10):
    response = requests.get(URL, params={'code':'000930','per':20,'page':i})
    soup = bs4.BeautifulSoup(response.text, "lxml")
    table = soup.table
    keys = [key.text for key in table.thead.find_all('th')]
    data0 = [[float(item.text) if isdigit(item.text) else item.text for item in row.find_all('td')] for row in table.tbody.find_all('tr')]
    data += data0

import pandas as pd
df = pd.DataFrame(data=data, columns=keys)
# df.to_excel('000930.xlsx') # save the data

x = df['累计净值']

import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot([xi for xi in x[1:]])
plt.show()
